﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace LIB
{
    public class JsWrite
    {

        #region alert and confirm


        public static void Alert(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');</script>");
        }

        public static void WriteJs(string jsCode)
        {
            HttpContext.Current.Response.Write("<script>" + jsCode + "</script>");
        }

 
        public static void AlertAndGoback(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');history.go(-1);</script>");
            HttpContext.Current.Response.End();
        }



        public static void alertAndCloseUpdateParent(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');if(window.parent){parent.location.reload(); }</script>");
             HttpContext.Current.Response.End();
        }

        public static void alertAndClose(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');if(window.parent){parent.layer.close(parent.layer.getFrameIndex(window.name));}</script>");
            HttpContext.Current.Response.End();
        }
        public static void LayerClose()
        {
            HttpContext.Current.Response.Write("<script>if(window.parent){parent.layer.close(parent.layer.getFrameIndex(window.name));}</script>");
            HttpContext.Current.Response.End();
        }
       
        public static void alertAndRefreshParent(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');window.parent.reload();</script>");
            HttpContext.Current.Response.End();
        }
        public static void alertAndRefreshSelf(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');</script>");
            HttpContext.Current.Response.End();
        }


        /// <summary>
        /// 弹出提示框，点确定后跳转
        /// </summary>
        /// <param name="page">当前页面this</param>
        /// <param name="msg">提示消息</param>
        /// <param name="gotourl">跳转页面</param>
        /// <param name="frm">跳转后的窗口：top this frmname</param>
        public static void AlertAndGo(System.Web.UI.Page page, string msg, string gotourl, string frm = "this")
        {
            page.Response.Write("<script>alert('" + msg + "');"+frm+".location.href='" + gotourl + "';</script>");
            page.Response.End();
        }
        public static void AlertAndGo(string msg, string gotourl, string frm = "this")
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');" + frm + ".location.href='" + gotourl + "';</script>");
            HttpContext.Current.Response.End();
        }
        

        public static void Confirm( string msg, string yesUrl, string noUrl)
        {
            if (noUrl == "")
            {
                if (yesUrl == "")
                    HttpContext.Current.Response.Write("<script>confirm('" + msg + "');</script>");
                else
                    HttpContext.Current.Response.Write("<script>if(confirm('" + msg + "'))location.href='" + yesUrl + "';</script>");
            }
            else
            {
                if (yesUrl == "")
                    HttpContext.Current.Response.Write("<script>if(!confirm('" + msg + "'))location.href='" + noUrl + "';</script>");
                else
                    HttpContext.Current.Response.Write("<script>if(confirm('" + msg + "'))location.href='" + yesUrl + "';else location.href='" + noUrl + "';</script>");
            }
        }


        public static void ConfirmOrClose(string msg)
        {
            HttpContext.Current.Response.Write("<script>if(!confirm('" + msg + "')){window.opener=null;window.close();}</script>");
            
        }
        #endregion

    }
}
